//
//  XSJComposePictureView.swift
//  weiBo项目准备
//
//  Created by 鲜世杰 on 16/5/19.
//  Copyright © 2016年 鲜世杰. All rights reserved.
//

import UIKit
private let XSJComposePictureViewCellIdentifier = "XSJComposePictureViewCellIdentifier"

class XSJComposePictureView: UICollectionView, UICollectionViewDataSource , UICollectionViewDelegate {
    
    var didSelectAddImageClosure : (()->())?
    
//    private lazy var images: [UIImage] = [UIImage]()
    lazy var images: [UIImage] = [UIImage]()

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        let flowLayout = UICollectionViewFlowLayout()
        
        super.init(frame: frame, collectionViewLayout: flowLayout)
        
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
        hidden = true
        
        registerClass(XSJComposePictureViewCell.self, forCellWithReuseIdentifier: XSJComposePictureViewCellIdentifier)
        delegate = self
        dataSource = self
        backgroundColor = RandomColor()
        
    }
    
    
    func addImage (image : UIImage) {
        
        hidden = false
        
        images.append(image)
        reloadData()
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let itemMargin: CGFloat = 5
        
        let itemWidth = (frame.size.width - 2 * itemMargin) / 3
        
        let flowLayout = self.collectionViewLayout as! UICollectionViewFlowLayout
        
        flowLayout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        
        flowLayout.minimumInteritemSpacing = itemMargin
        flowLayout.minimumLineSpacing = itemMargin
        
    }
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//        return images.count
        
        let count = images.count
        if count == 0 || count == 9 {
        
            return count
            
        }else{
        
            return count + 1
            
        }
        
        
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(XSJComposePictureViewCellIdentifier, forIndexPath: indexPath) as! XSJComposePictureViewCell
//        cell.image = images[indexPath.item]
        
        if indexPath.item < images.count {
        
            cell.image = images[indexPath.item]
            
        }else{
        
            cell.image = nil
            
        }
        
        cell.deleteButtonClosure = {[weak self] in
        
            self?.images.removeAtIndex(indexPath.item)
            
            if self?.images.count == 0 {
                self?.hidden = true
            }
            self?.reloadData()

            
        }
        
        
//        cell.backgroundColor = RandomColor()
        return cell
        
        
    }
    
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        

        collectionView.deselectItemAtIndexPath(indexPath, animated: true)
        
        if indexPath.item == images.count{
        
            didSelectAddImageClosure?()
            
        }
    }
    
}

class XSJComposePictureViewCell : UICollectionViewCell{
    
    var deleteButtonClosure: (()->())?

    
    var image : UIImage? {
        didSet {
            
            if image == nil{
            
                imageView.image = UIImage(named: "compose_pic_add")
                
                imageView.highlightedImage = UIImage(named: "compose_pic_add_highlighted")
                
                deleteImageButton.hidden = true


            }else{
            
                imageView.image = image
                imageView.highlightedImage = nil
                deleteImageButton.hidden = false
            }
            
            
        }
    }
    private lazy var imageView: UIImageView = UIImageView(image: UIImage(named: "timeline_image_placeholder"))
    
    
    private lazy var deleteImageButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: "deleteImageButtonClick", forControlEvents: .TouchUpInside)
        button.setImage(UIImage(named: "compose_photo_close"), forState: .Normal)
        return button
    }()

    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpui()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUpui() {
        
        contentView.addSubview(imageView)
        
        contentView.addSubview(deleteImageButton)
        
        imageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        deleteImageButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(imageView)
            make.trailing.equalTo(imageView)
        }
    }
  
    @objc private func deleteImageButtonClick() {

        deleteButtonClosure?()

    }
    
}
